<div class="col-mg-12" style="text-align: center;">
    <h3>目录管理</h3>
</div>

<div class="col-md-12 tabs-div">
    <ul class="nav nav-tabs">
        <li role="presentation" class="active"><a href="/sys/menu">目录列表</a></li>
        <!--
        <li role="presentation"><a href="/sys/menu/power">目录权限</a></li>
        -->
    </ul>
</div>
<div class="col-sm-12">
    <form action="/sys/menu" method="get">
        <div class="col-md-5">
            <div class="form-group col-md-10">
                <div class="input-group">
                    <div class="input-group-addon">目录分组名称:</div>
                    <input type="text" class="form-control col-md-7" name="menu_title" id="" placeholder="请输入目录名称" value="<?= !empty($_params['menu_title']) ? $_params['menu_title'] : '' ?>" >
                </div>
            </div>
        </div>

        <div class="col-md-1" >
            <div class="form-group">
                <input type="submit" class="btn btn-sm btn-info" value="搜索">
            </div>
        </div>
        <div class="col-md-1" style="text-align: right;">
            <!--<a onclick="downloadfile()" class="btn btn-sm btn-info">导出表格</a>-->
        </div>
    </form>
</div>

<table class="table table-hover tableWithBorder">
    <thead>
    <tr>
        <th>分组</th>
        <th>页面ID</th>
        <th>标题</th>
        <th>URI</th>
        <th>菜单等级</th>
        <th>更新时间</th>
        <th>组内排序</th>
        <th>操作</th>
    </tr>
    </thead>
    <tbody>
    <?php
    foreach ($list as $menu) {
        ?>
        <tr class="level-father">
            <td>
                <button class="btn btn-default btn-sm btn-info sons-ctr-btn" data-id="<?=$menu['id']?>" data-status="close"> + </button>
            </td>
            <td><?=$menu['level_sign']?></td>
            <td><?=$menu['title']?></td>
            <td><?=$menu['uri'] ? : '无'?></td>
            <td><?=$menu['level']?></td>
            <td><?=date("Y-m-d H:i:s", $menu['created'])?></td>
            <td>
                <?=$menu['order_index']?>
            </td>
            <td>
                <a class="btn btn-default btn-sm btn-info" href="/sys/menu/power?menu_id=<?=$menu['id']?>">管理</a>
                <button class="btn btn-default btn-sm btn-info move" data-forward="up" data-id="<?=$menu['id']?>" data-parent="zero">上移</button>
                <button class="btn btn-default btn-sm btn-info move" data-forward="down" data-id="<?=$menu['id']?>" data-parent="zero">下移</button>
            </td>
        </tr>
        <?php
        if (!empty($menu['son_list'])) {
            foreach ($menu['son_list'] as $sonMenu) {
                ?>
                <tr class="level-son hidden son-list-<?=$menu['id']?>">
                    <td></td>
                    <td><?=$sonMenu['level_sign']?></td>
                    <td><?=$sonMenu['title']?></td>
                    <td><?=$sonMenu['uri']?></td>
                    <td><?=$sonMenu['level']?></td>
                    <td><?=date("Y-m-d H:i:s", $sonMenu['created'])?></td>
                    <td><?=$sonMenu['order_index']?></td>
                    <td>
                        <a class="btn btn-default btn-sm btn-info" href="/sys/menu/power?menu_id=<?=$sonMenu['id']?>">管理</a>
                        <button class="btn btn-default btn-sm btn-info move" data-forward="up" data-id="<?=$sonMenu['id']?>" data-parent="<?=$menu['id']?>">上移</button>
                        <button class="btn btn-default btn-sm btn-info move" data-forward="down" data-id="<?=$sonMenu['id']?>" data-parent="<?=$menu['id']?>">下移</button>
                    </td>
                </tr>
                <?php
            }
        }
    }
    ?>
    </tbody>
</table>

<script>
    var ajaxUrl = '/sys/menu/ajax';
    $(function() {
        $(".sons-ctr-btn").click(function(){
            var menuId = $(this).data('id');
            var menuStatus = $(this).data('status');

            if (menuStatus == 'close') {
                $(".son-list-" + menuId).removeClass('hidden');
                $(this).html(' - ');
                $(this).data('status', 'open');
            } else {
                $(".son-list-" + menuId).addClass('hidden');
                $(this).html(' + ');
                $(this).data('status', 'close');
            }
        });

        $(".move").click(function(){
            var moveType    = $(this).data('forward');
            var menuId      = $(this).data('id');
            var parentId    = $(this).data('parent');

            var postData = {
                action  : 'orderMenu',
                move    : moveType,
                menu_id : menuId,
                parent_id : parentId
            };
            console.log(postData);

            $.ajax({
                type: "POST",
                url: ajaxUrl,
                data: postData,
                dataType: "json",
                success:function(resp) {
                    if(resp.status == 200) {
                        alert("操作成功");
                        window.location.reload();
                    } else {
                        alert(resp.msg);
                        window.location.reload();
                    }
                }
            });
        });
    });
</script>